package com.madou.dbtool.redis.node;


import com.madou.common.annotation.HideClass;
import com.madou.dbtool.common.enums.DBEnum;
import com.madou.dbtool.common.icon.AllIcons;
import com.madou.dbtool.common.messages.DBToolBundle;
import com.madou.dbtool.event.DBEventManager;
import com.madou.dbtool.redis.constants.RedisIds;
import com.madou.dbtool.redis.event.OpenRedisClientEvent;
import com.madou.dbtool.redis.event.OpenRedisDataEvent;
import com.madou.dbtool.redis.event.OpenRedisMonitorEvent;
import com.madou.dbtool.redis.event.OpenRedisSlowLogEvent;
import com.madou.dbtool.redis.manager.RedisNodeInfo;
import com.madou.dbtool.redis.manager.RedisSourceManager;
import com.madou.dbtool.relationship.common.BaseNonRelationNodeParm;
import com.madou.dbtool.relationship.node.menu.NodeMenuFactory;
import com.madou.dbtool.ui.tree.DataSourceNode;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.sql.SQLException;
import java.util.*;

/**
 * @author miukoo
 * Redis服务器信息节点
 */
@HideClass
public class RedisServerNode extends BaseNonRelationNodeParm implements RedisTreeNodeInfo {
    //表的菜单
    private static List<JMenuItem> REDIS_NODE_MENU = new ArrayList<>();
    static {
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.reload"),114,114, AllIcons.MENU.REFRESH));
        REDIS_NODE_MENU.add(null);
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.dataview"),68,68,AllIcons.Common.TABLE));
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.client"),67,67,AllIcons.Common.TERMINAL));
        REDIS_NODE_MENU.add(null);
//        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.monitor"),77,77,AllIcons.Common.QUERY_LOG));
        REDIS_NODE_MENU.add(createMenu(DBToolBundle.message("ds.tree.redis.server.node.slowlog"),83,83,AllIcons.Common.SLOW_LOG));
    }
    // 自带数据源ID
    String id = UUID.randomUUID().toString();
    // 数据库类型
    DBEnum type = DBEnum.REDIS;
    // 资源管理器
    RedisSourceManager redisSourceManager;
    // 节点新
    RedisNodeInfo redisNodeInfo;
    // 数据库数量
    int count = 0;
    Map<Integer,RedisDataBaseNode> cache = new HashMap<>();

    public RedisServerNode(DataSourceNode parent, RedisNodeInfo redisNodeInfo) {
        super(parent);
        this.redisSourceManager = redisNodeInfo.getRedisSourceManager();
        this.redisNodeInfo = redisNodeInfo;
    }

    @Override
    public DataSourceNode getChildren(DataSourceNode parent, int index) {
        RedisDataBaseNode redisDataBaseNode = cache.get(index);
        if(redisDataBaseNode==null){
            redisDataBaseNode = new RedisDataBaseNode(this,redisSourceManager,redisNodeInfo,index);
            cache.put(index,redisDataBaseNode);
        }
        return redisDataBaseNode;
    }

    @Override
    public String getText() {
        return redisNodeInfo.getIp()+":"+redisNodeInfo.getPort();
    }

    @Override
    public Icon getIcon() {
        return AllIcons.Common.NODE;
    }

    @Override
    public int countChild() {
        if(!open) {
            count = redisSourceManager.getDBCount();
            open = true;
        }
        return count;
    }

    @Override
    public boolean hasChild() {
        return true;
    }

    /**
     * 子元素右键菜单点击
     * @return
     */
    @Override
    public void clickMenu(JTree jTree, TreePath treePath,JMenuItem jMenuItem){
        // 广播事件
        if(isMenuCode(jMenuItem,1)){
            reload(jTree,treePath);
        }
        if(isMenuCode(jMenuItem,67)){
            DBEventManager.notice(new OpenRedisClientEvent(RedisIds.EVENT_ID_CLIENT+"0",redisSourceManager,redisNodeInfo));
        }
        if(isMenuCode(jMenuItem,77)){
            DBEventManager.notice(new OpenRedisMonitorEvent(RedisIds.EVENT_ID_MONITOR,redisSourceManager,redisNodeInfo));
        }
        if(isMenuCode(jMenuItem,83)){
            DBEventManager.notice(new OpenRedisSlowLogEvent(RedisIds.EVENT_ID_SLOW,redisSourceManager,redisNodeInfo));
        }
        if(isMenuCode(jMenuItem,68)){
            DBEventManager.notice(new OpenRedisDataEvent(RedisIds.EVENT_ID_DATAVIEW+"0",redisSourceManager,redisNodeInfo,null));
        }
    }

    @Override
    public void reload(JTree jTree, TreePath treePath){
        count=0;
        open=false;
        cache.clear();
        super.reload(jTree,treePath);
    }

    /**
     * 子元素右键菜单
     * @return
     */
    @Override
    public List<JMenuItem> rightMenu(){
        List temp = new ArrayList();
        for (JMenuItem dbMenu : REDIS_NODE_MENU) {
            temp.add(NodeMenuFactory.copyMenuItem(dbMenu));
        }
        return temp;
    }

    /**
     * 是否自动展开
     * @return
     */
    @Override
    public boolean autoExpand(){
        return countChild()==1;
    }

    @Override
    public RedisSourceManager getRedisSourceManager() {
        return this.redisSourceManager;
    }

    @Override
    public RedisNodeInfo getRedisNodeInfo() {
        return this.redisNodeInfo;
    }

}
